<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2008                                               |
| http://e107shop.com                                                    |
| patrick@byorder.nl                                                     |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
|                                                                        |
| $Revision: 1.0                                                         |
| $Date: 2008/08/20                                                      |
| $Author: Bieleke                                                       |
+ ---------------------------------------------------------------------- +
*/

function e107shop_checkout_details($cart_id){
    global $e107shop_db_name, $shop_settings, $tp;
    $cart_id = $tp->toDB($cart_id);
    $sql=new db;
    $checkout = "<table class='fborder' width='100%'><tr>
    <td class='forumheader' style='text-align: left' colspan='2'>&nbsp;".lan_global_nav_to."
    <a href='".$shop_settings['path']."index.php'>"
    .lan_global_mainpage."</a> --> <a href='cart.php?action=view'>"
    .lan_global_shopping_cart."</a> --> <span style='color: #ec5800; font-weight: bold;'>"
    .lan_global_checkout."</span></td></tr>";
    
    //
    // Select the user information from the table
    //
    if(USER){
        // This will grab the entries from the SQL database for e107Shop
        if ($sql->db_Select($e107shop_db_name['customer_information'], "*", "username='".USERNAME."'")) {
            while($row = $sql -> db_Fetch()){
                // Statement 1, saying... if the customer exists... use the SQL e107Shop database to 
                $customer_name=$row["customer_name"];
                $customer_last_name=$row["customer_last_name"];
                $customer_email=$row["customer_email"];
                $customer_address1=$row["customer_address1"];
                $customer_address2=$row["customer_address2"];
                $customer_city=$row["customer_city"];
                $customer_state=$row["customer_state"];
                $customer_country=$row["customer_country"];
                $customer_region=$row["customer_region"];
                $customer_zip=$row["customer_zip"];
            }
        } else {
            // If statement 1 doesn't work, use the tables from the e107 member table.
            if ($sql->db_Select("user", "*", "user_name='".USERNAME."'")) {
                while($row = $sql -> db_Fetch()){
                    //
                    // Fetch the details and store them in variables so the HTML only needs to be written once
                    //
                    $customer_email=$row["user_email"];
                }
            }
        }
    }

    
    //
    // Start Validation
    //
    if(isset($_POST["pay"])) {
        $customer_name = $tp->toDB($_POST["customer_name"]);
        $customer_last_name = $tp->toDB($_POST["customer_last_name"]);
        $customer_email = $tp->toDB($_POST["customer_email"]);
        $customer_address1 = $tp->toDB($_POST["customer_address1"]);
        $customer_address2 = $tp->toDB($_POST["customer_address2"]);
        $customer_city = $tp->toDB($_POST["customer_city"]);
        $customer_state = $tp->toDB($_POST["customer_state"]);
        $customer_country = $tp->toDB($_POST["customer_country"]);
        $customer_region = $tp->toDB($_POST["customer_region"]);
        $customer_zip = $tp->toDB($_POST["customer_zip"]);
        $error_msg='';
        if(e107shop_get_validate(vald_fname)=='1'){
            if(trim($_POST["customer_name"])=='') {
                $error_msg .= "Please enter your First name<br>";
            }
        }
        if(e107shop_get_validate(vald_lname)=='1'){
            if(trim($_POST["customer_last_name"])=='') {
                $error_msg .= "Please enter your Last name<br>";
            }
        }
        if(e107shop_get_validate(vald_addr1)=='1'){
            if(trim($_POST["customer_address1"])=='') {
                $error_msg .= "Please enter your Street Address<br>";
            }
        }
        if(e107shop_get_validate(vald_city)=='1'){
            if(trim($_POST["customer_city"])=='') {
                $error_msg .= "Please enter your City<br>";
            }
        }
        if(e107shop_get_validate(vald_state)=='1'){
            if(trim($_POST["customer_state"])=='') {
                $error_msg .= "Please enter your State<br>";
            }
        }
        if(e107shop_get_validate(vald_count)=='1'){
            if(trim($_POST["customer_country"])=='') {
                $error_msg .= "Please enter your Country<br>";
            }
        }
        if(e107shop_get_validate(vald_region)=='1'){
            if(trim($_POST["customer_region"])=='') {
                $error_msg .= "Please enter your Region<br>";
            }
        }
        if(e107shop_get_validate(vald_zip)=='1'){
            if(trim($_POST["customer_zip"])=='') {
                $error_msg .= "Please enter your Zip Code<br>";
            }
        }
        if(e107shop_get_validate(vald_email)=='1'){
            if(trim($_POST["customer_email"])=='') {
                $error_msg .= "Please enter an E-mail Address<br>";
            } else {
                // check if email is a valid address in this format username@domain.com
                if(!ereg("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]", 
                $_POST["customer_email"])) $error_msg.="Please enter a valid email address<br>";
            }
        }

        // display error message if any, if not, proceed to other processing
        if ($error_msg == '') {
            $error_num = 1;
        } else {
            $vald_error = "<font color=red>$error_msg</font>";
        }
    }

    //
    // Write the form
    //
    if($error_num == 1) {
        return require("payment.php");
    } else {
        $checkout .= "<form action='checkout.php' method='post'><tr><td colspan='2'>&nbsp;</td></tr>";
        $checkout .= $vald_error;
    }
        
    //
    // Display the fields required for the customers details
    //
    $validate_required = "<span style='color=red;'>*</span>";
    
    $checkout .= "<tr><td width='20%' style='text-align: right; font-weight: bold;'>".lan_name;
    if(e107shop_get_validate(vald_fname)=='1') $checkout .= $validate_required;
    $checkout .= "</td><td class='forumheader2' width='80%'>
    <input type='text' name='customer_name' value='".$customer_name."' class='tbox' size='60'></td></tr>";
    
    $checkout .= "<tr><td width='20%' style='text-align: right; font-weight: bold;'>".lan_last_name;
    if(e107shop_get_validate(vald_lname)=='1') $checkout .= $validate_required;
    $checkout .= "</td><td class='forumheader2' width='80%'>
    <input type='text' name='customer_last_name' value='".$customer_last_name."' class='tbox' size='60'></td></tr>";
    
    $checkout .="<tr><td  width='20%' style='text-align: right; font-weight: bold;'>".lan_email;
    if(e107shop_get_validate(vald_email)=='1') $checkout .= $validate_required;
    $checkout .= "</td><td class='forumheader2' width='80%'>
    <input type='text' name='customer_email' value='".$customer_email."' class='tbox' size='60'></td></tr>";

    $checkout .= "<tr><td  width='20%' style='text-align: right; font-weight: bold;'>".lan_address;
    if(e107shop_get_validate(vald_addr1)=='1') $checkout .= $validate_required;
    $checkout .= "</td><td class='forumheader2' width='80%'>
    <input type='text' name='customer_address1' value='".$customer_address1."' class='tbox' size='60'></td></tr>";

    $checkout .= "<tr><td width='20%'>&nbsp;</td><td class='forumheader2' width='80%'>
    <input type='text' name='customer_address2' value='".$customer_address2."' class='tbox' size='60'></td></tr>";
    
    $checkout .="<tr><td  width='20%' style='text-align: right; font-weight: bold;'>".lan_zip;
    if(e107shop_get_validate(vald_zip)=='1') $checkout .= $validate_required;
    $checkout .="</td><td class='forumheader2' width='80%'>
    <input type='text' name='customer_zip' value='".$customer_zip."' class='tbox' size='60'></td></tr>";
    
    $checkout .="<tr><td  width='20%' style='text-align: right; font-weight: bold;'>".lan_city;
    if(e107shop_get_validate(vald_city)=='1') $checkout .= $validate_required;
    $checkout .= "</td><td class='forumheader2' width='80%'>
    <input type='text' name='customer_city' value='".$customer_city."' class='tbox' size='60'></td></tr>";
    
    $checkout .="<tr><td  width='20%' style='text-align: right; font-weight: bold;'>".lan_state;
    if(e107shop_get_validate(vald_state)=='1') $checkout .= $validate_required;
    $checkout .= "</td><td class='forumheader2' width='80%'>
    <input type='text' name='customer_state' value='".$customer_state."' class='tbox' size='60'></td></tr>";

    $checkout .="<tr><td  width='20%' style='text-align: right; font-weight: bold;'>".lan_country;
    if(e107shop_get_validate(vald_count)=='1') $checkout .= $validate_required;
    $checkout .= "</td><td class='forumheader2' width='80%'>
    <input type='text' name='customer_country' value='".$customer_country."' class='tbox' size='60'></td></tr>";

    if($shop_settings["postage_method"] == "3" || $shop_settings["postage_method"] == "4"){
        $checkout .="<tr><td colspan='2'>&nbsp;</td></tr><tr><td  width='20%' style='text-align: right; font-weight: bold;'>".lan_region;
        if(e107shop_get_validate(vald_region)=='1') $checkout .= $validate_required;
        $checkout .= "</td><td class='forumheader2' width='80%'>".e107shop_get_regions($customer_region)
        ."&nbsp;&nbsp;<img src='images/text_arrow.png' alt='' />
        &nbsp;&nbsp;<span style='color: #CE0007; font-weight: bold;'>".lan_region_help."</span></td></tr>";
    } else if($shop_settings["postage_method"] == "1" || $shop_settings["postage_method"] == "2") {
        $checkout .="<tr><td colspan='2'><input type='hidden' name='postage' value='".$_POST["postage"]."' /></td></tr>";
    }
    
    $checkout .="<tr><td colspan='2'>&nbsp;</td></tr><tr><td  width='20%' style='text-align: right; font-weight: bold;'>".lan_payment_method
    ."</td><td class='forumheader2' width='80%'>".e107shop_payment_methods()."&nbsp;&nbsp;&nbsp;<img src='images/text_arrow.png' alt='' />
    &nbsp;&nbsp;<span style='color: #CE0007; font-weight: bold;'>".lan_payment_method_help."</span></td></tr>";
    
    if($shop_settings["use_coupons"] == 1) {
        $checkout .="<tr><td colspan='2'>&nbsp;</td></tr><tr><td width='20%' style='text-align: right; font-weight: bold;'>".lan_coupon_code
        ."</td><td class='forumheader2' width='80%'><input type='text' name='coupon' class='tbox'>
        &nbsp;&nbsp;<img src='images/text_arrow.png' alt='' />
        &nbsp;&nbsp;<span style='color: #CE0007; font-weight: bold;'>".lan_coupon_code_help."</span></td></tr>";
    }

    $checkout .="<tr><td colspan='2'>&nbsp;</td></tr><tr><td class='forumheader' width='100%' colspan='2'><center>
    <input type='submit' name='pay' value='".lan_proceed."' /></td></tr>";
    $checkout .="</form></table>";

    return $checkout;

}


function e107shop_get_regions($customer_region=1){
    global $e107shop_db_name;
    $customer_region = (int)$customer_region;
    $region = "<select name='customer_region' class='tbox'>";
    $sql=new db;
    $sql->db_Select($e107shop_db_name['postage'], "*", "");
    while($row = $sql->db_Fetch()){
        if ($customer_region == $row['region_id']) {
            $region .="<option value='".$row["region_id"]."' selected='selected'>".$row["postage_region"]."</option>";
        } else {
            $region .="<option value='".$row["region_id"]."'>".$row["postage_region"]."</option>";
        }
    }
    $region .="</select>";

    return $region;
}


function e107shop_get_validate($field){
    global $e107shop_db_name, $tp;
    $field = $tp->toDB($field);
    $sql = new db;
    $sql->db_Select($e107shop_db_name['validate'], $field, "");
        while($row = $sql->db_Fetch()){
            $field_data = $row[$field];
        }
    return $field_data;
}
